﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.Control;
using System.Xml;
using System.Collections;
using DispatchAssist.Service;
using System.Web.Services.Protocols;
using DispatchAssist.Model;
using CarolLib;

namespace DispatchAssist.AppHost
{
    [ModuleContract("49E5B2FE-4A9B-4F96-9AB4-BE0F9D673B99", "日志管理", "提醒日志查询")]
    public partial class QueryActionPerformLogManage : MdiUserControl
    {
        AuthService service = new AuthService();
        List<QueryActLog> apflst = null;

        int previewIndex = 0;
        public QueryActionPerformLogManage()
            : base()
        {
            InitializeComponent();
        }
        public QueryActionPerformLogManage(XmlNode xn)
            : this()
        {
        }



        private void BindDataGrid(QueryCondition qc)
        {

            try
            {
                //List<QueryCondition> qclst = new List<QueryCondition>();
                //QueryCondition qc = new QueryCondition();

                ////qc.StartTime = new DateTime();
                //qc.StartTime = dtpStartTime.Value;
                ////qc.EndTime = new DateTime();
                //qc.EndTime = dtpEndTime.Value;
                //object obj = null;
                ///if (qclst == null)

                WaitForm wf = new WaitForm();
                wf.StartPosition = FormStartPosition.CenterParent;
                //object obj = service.QueryLogCommon("ACTIONPERFORMLOG", qc);

                service.QueryLogCommonAsync("ACTIONPERFORMLOG", qc, wf);

                wf.ShowDialog();

            }
            catch (SoapHeaderException soapEx)
            {
                MessageBox.Show("SoapHeader Error : " + soapEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        void service_QueryLogCommonCompleted(object sender, QueryLogCommonCompletedEventArgs e)
        {
            string jsonstr = (string)e.Result;
            try
            {
                apflst = JsonSerializeHelper.DataContractJsonDeserializeByJNet<List<QueryActLog>>(jsonstr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            //else
            //    obj = service.QueryLogCommon("ACTIONPERFORMLOG");

            if (apflst == null) { ((WaitForm)e.UserState).Close(); return; }
            dgActPerfromList.AutoGenerateColumns = false;
            dgActPerfromList.DataSource = apflst;

            ((WaitForm)e.UserState).Close();
        }







        private void BindOperaters()
        {
            // Dictionary<string, string> dic = new Dictionary<string, string>();

            List<SeatUser> users = service.GetAllSeatUser(1).ToList<SeatUser>();

            //foreach (SeatUser su in users)
            //{
            //    dic.Add(su.UserID.ToString(), su.UserName);

            //}

            //users.ToDictionary<SeatUser, string>(s => s.UserID.ToString());
            SeatUser su = new SeatUser();
            su.UserID = new Guid();
            su.UserName = "未选择";
            users.Insert(0, su);
            cmbOperater.DataSource = users == null ? new List<SeatUser>() : users;
            cmbOperater.DisplayMember = "UserName";
            cmbOperater.ValueMember = "UserID";

        }
        public override void Onload()
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();
            service.QueryLogCommonCompleted += new QueryLogCommonCompletedEventHandler(service_QueryLogCommonCompleted);

            BindOperaters();
            // List<QueryCondition> qclist = new List<QueryCondition>();
            dtpStartTime.Value = DateTime.Now;
            dtpEndTime.Value = DateTime.Now;
            QueryCondition qc = new QueryCondition();
            qc.StartTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            qc.EndTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            qc.UserID = "-1";
            qc.UserName = "-1";
            BindDataGrid(qc);
            base.Onload();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {

            if (Convert.ToDateTime(dtpStartTime.Value.ToShortDateString()) > Convert.ToDateTime(Convert.ToDateTime(dtpEndTime.Value.ToShortDateString())))
            {
                MessageBox.Show("开始时间不能大于结束时间！");
                return;
            }
            QueryCondition qc = new QueryCondition();

            //System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);
            //DateTime.ParseExact(dtpStartTime.Text, "yyyyMMddHHmmss", format);
            qc.StartTime = Convert.ToDateTime(dtpStartTime.Value.ToString("yyyy-MM-dd 00:00:00"));
            qc.EndTime = Convert.ToDateTime(dtpEndTime.Value.ToString("yyyy-MM-dd 23:59:59"));
            if (cmbOperater.Text == "未选择") { qc.UserID = "-1"; qc.UserName = "-1"; }
            else { qc.UserName = cmbOperater.Text; }//qc.UserID = cmbOperater.SelectedValue.ToString(); 
            BindDataGrid(qc);

        }

        private void dgActPerfromList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewColumn column = dgActPerfromList.Columns[e.ColumnIndex];

            Guid alertID = (Guid)dgActPerfromList["AlertID", e.RowIndex].Value;


            if (column is DataGridViewLinkColumn)
            {
                frmViewAlertActLogDetail frm = new frmViewAlertActLogDetail((QueryActLog)dgActPerfromList.Rows[e.RowIndex].DataBoundItem);
                frm.StartPosition = FormStartPosition.CenterParent;
                if (frm.isViewDialog) frm.ShowDialog();
                else MessageBox.Show("无明细信息");
            }
        }

    }
}
